package com.yaorange.nongjt.common.utils.nongjt;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class QiniuUtils {

    private static final Logger log = LoggerFactory.getLogger(QiniuUtils.class);

    /**
     * AK
     */
    private static String accessKey = "1ojrMNcuAs7NMdVyKrxF9Bm9J6tQiLZ0ThCkr5YL";
    /**
     * SK
     */
    private static String secretKey = "CItA77A_LGXxNJ6shDmO_gygc__HuAsRkMmm8PK9";
    /**
     * 空间名称
     */
    private static String bucket = "njt46";
    /**
     * 外链域名
     */
    private static String outerUrl = "http://slfkdy370.hn-bkt.clouddn.com/";

    /**
     * 文件上传
     *
     * @param file     上传文件
     * @param fileName 文件名
     * @return 文件上传成功地址
     */
    public static String upload(MultipartFile file, String fileName) {
        try {
            return upload(file.getInputStream(), fileName);
        } catch (Exception e) {
            log.error("{} 文件上传失败：{}", fileName, e);
            return null;
        }
    }

    /**
     * 文件上传
     *
     * @param inputStream 上传文件流
     * @param fileName    文件名
     * @return 文件上传成功地址
     */
    public static String upload(InputStream inputStream, String fileName) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        // 构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        try {
            Response response = uploadManager.put(inputStream, fileName, upToken, null, null);
            // 解析上传成功的结果
            DefaultPutRet putRet = response.jsonToObject(DefaultPutRet.class);
            if (null != putRet) {
                return outerUrl + putRet.key;
            }
        } catch (Exception e) {
            log.error("{} 文件上传失败：{}", fileName, e);
        }
        return null;
    }


    /**
     * 删除
     *
     * @param fileName 文件名
     * @return true 删除成功，false 删除失败
     */
    public static Boolean remove(String fileName) {
        Auth auth = Auth.create(accessKey, secretKey);
        Configuration cfg = new Configuration(Region.region2());
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
            return Boolean.TRUE;
        } catch (QiniuException e) {
            log.error("{} 文件删除失败：{}", fileName, e);
            return Boolean.FALSE;
        }

    }

    public static void main(String[] args) {

        try {
            // 测试上传
            File file = new File("C:\\1.png");
            FileInputStream fileInputStream = new FileInputStream(file);
            String filePath = upload(fileInputStream, file.getName());
            System.out.println(filePath);

            // 测试删除
            // remove("菜油.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
